package com.example.demo.controller;

import com.example.demo.dto.MomentDetailDTO;
import com.example.demo.dto.PopularDTO;
import com.example.demo.entity.Moment;
import com.example.demo.entity.PopularRecord;
import com.example.demo.entity.ReciteNum;
import com.example.demo.mapper.MomentMapper;
import com.example.demo.service.CommunityService;
import com.example.demo.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.naming.Context;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@CrossOrigin
@RequestMapping("/community")
@Slf4j
public class CommunityController {

    @Resource
    private CommunityService communityService;

    @Resource
    private MomentMapper momentMapper;

    @PostMapping("/insertMoment")
    public Result insertMoment(@RequestBody Moment moment) {
        Integer integer = communityService.insertMoments(moment);
        if (integer > 0) {
            return Result.ok("发表成功");
        } else {
            return Result.error("发表失败");
        }
    }

    @GetMapping("/selectMoments")
    public Result selectMoments(Integer pageNo) {
        List<Moment> list = communityService.selectAll(pageNo);
        return Result.ok().data("list", list);
    }


    /**
     * 图片上传
     */
    @RequestMapping(value = "/uploadImage", method = RequestMethod.POST)
    @ResponseBody
    public Result uploadImage(@RequestParam(value = "avatar") MultipartFile avatar) {
        return communityService.insertPic(avatar);
    }

    /**
     * 动态内容模糊匹配
     */
    @GetMapping("/contentPart")
    @ResponseBody
    public Result uploadImage(String contentPart, Integer pageNo) {
        List<Moment> list = communityService.findByContentPart(contentPart, pageNo);
        return Result.ok().data("list", list);
    }

    /**
     * 根据用户id返回动态，按照时间排序
     */
    @GetMapping("/myMoment")
    @ResponseBody
    public Result myMoment(Long userId, Integer pageNo) {
        List<Moment> list = communityService.findMyMoment(userId, pageNo);
        return Result.ok().data("list", list);
    }

    /**
     * 根据id查询动态详细信息
     */
    @GetMapping("/momentDetail")
    @ResponseBody
    public Result momentDetail(Long id, Integer pageNo) {
        MomentDetailDTO momentDetailDTO = communityService.selectMomentDetail(id, pageNo);
        return Result.ok().data("momentDetailDTO", momentDetailDTO);
    }

    /**
     * 用户活跃度排名
     */
    @GetMapping("/popularRank")
    @ResponseBody
    public Result popularRank() {
        /*这里服用ReciteNum，简便操作，不重复同样的pojo类*/
        List<ReciteNum> momentList  = communityService.getRank();
        List<ReciteNum> commentList = momentMapper.selectRank();
        /*获取所有去重的username List*/
        List<String> collect = momentList.stream().map(ReciteNum::getUsername).collect(Collectors.toList());
        List<String> collect1 = commentList.stream().map(ReciteNum::getUsername).collect(Collectors.toList());
        collect.addAll(collect1);
        int size = collect.size();
        /*获取Map以加快检索效率*/
        Map<String,Integer> momentMap = new HashMap<>();
        Map<String,Integer> commentMap = new HashMap<>();
        for (ReciteNum reciteNum : momentList) {
            momentMap.put(reciteNum.getUsername(),reciteNum.getReciteNum());
        }
        for (ReciteNum reciteNum : commentList) {
            commentMap.put(reciteNum.getUsername(),reciteNum.getReciteNum());
        }
        List<PopularRecord> result = new ArrayList<>();
        for (String s : collect) {
            PopularRecord popularRecord = new PopularRecord(s, 0, 0);
            Integer momentNum = momentMap.get(s);
            Integer commentNum = commentMap.get(s);
            if (momentNum!=null){
                popularRecord.setMomentNum(momentNum);
            }
            if (commentNum!=null){
                popularRecord.setCommentNum(commentNum);
            }
            result.add(popularRecord);
        }
        int size1 = result.size();
        String[] usernameArray  = new String[size1];
        Integer[] commentArray = new Integer[size1];
        Integer[] momentArray = new Integer[size1];
        for (int i = 0; i < size1; i++) {
            usernameArray[i] = result.get(i).getUsername();
            commentArray[i] = result.get(i).getCommentNum();
            momentArray[i] = result.get(i).getMomentNum();
        }
        PopularDTO popularDTO = new PopularDTO(usernameArray,commentArray,momentArray);
        return Result.ok().data("popularDTO", popularDTO);
    }
}
